﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Text;

/// <summary>
/// static类
/// 存放通用函数
/// 函数分类：
///     设置gameobject active
///     设置MonoBehaviour enable
/// </summary>
public static class CommonFunctions
{
    static public T CreateGO<T>(T t) where T : MonoBehaviour
    {
        GameObject go = GameObject.Instantiate(t.gameObject) as GameObject;
        var ret = go.GetComponent<T>();

        return ret;
    }

    static public void ResetTransform(Transform t, Transform parent = null)
    {
        if (parent != null)
            t.parent = parent;

        t.localPosition = Vector3.zero;
        t.localScale = Vector3.one;
        t.localEulerAngles = Vector3.zero;
    }

    /// <summary>
    /// 将publickey转化成数字
    /// </summary>
    static public uint TranslatePublicKey(string key)
    {
        uint ret = 0;
        foreach (char c in key)
        {
            ret += c;
        }

        return ret;
    }

    public static int[] ParseIntArr(string s, char[] char_sp)
    {
        if (s == "-1")
            return new int[] { };
        string[] ret = s.Split(char_sp, System.StringSplitOptions.RemoveEmptyEntries);
        if (ret.Length <= 0)
        {
            Debugger.LogError("error s : " + s); // ret.length > 0
            return null;
        }
        int[] arr = new int[ret.Length];

        for (int i = 0; i < ret.Length; i++)
        {
            arr[i] = int.Parse(ret[i]);
        }

        return arr;
    }
    /// <summary>
    /// 去除注释;
    /// </summary>
    /// <param name="dict"></param>
    static public void FilterCommend(Dictionary<string, string> dict)
    {
        Debugger.Assert(dict != null, "error ");
        var keys = new List<string>(dict.Keys);

        foreach (var key in keys)
        {
            string v = dict[key];
            int index = v.IndexOf("//");
            if (index > 0)
                v = v.Remove(index);
            dict[key] = v.Trim();
        }
    }

    #region 执行回调函数
    static public void CallAction(System.Action act)
    {
        if (act != null)
        {
            var tmp = act;
            tmp();
        }
    }

    static public void CallAction<T>(System.Action<T> act, T p)
    {
        if (act != null)
        {
            var tmp = act;
            tmp(p);
        }
    }

    static public void CallAction<T1, T2>(System.Action<T1, T2> act, T1 p1, T2 p2)
    {
        if (act != null)
        {
            var tmp = act;
            tmp(p1, p2);
        }
    }

    #endregion
}
